﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace SmartMD.Manager.Patient
{
    /// <summary>
    /// Interaction logic for QueryInfoListPad.xaml
    /// </summary>
    public partial class QueryInfoListPad : UserControl
    {
        public ObservableCollection<PatientInfo> patientPrintList = new ObservableCollection<PatientInfo>(); //打印列表
        private ObservableCollection<PatientInfo> _patientList;
        public ObservableCollection<PatientInfo> PatientList
        {
            get
            {
                return _patientList;
            }
            set
            {
                if (value != null)
                {
                    _patientList = value;
                    for (int i = 0; i < _patientList.Count; i++)
                    {
                        _patientList[i].State = DataModel.RecordState.Normal;
                    }
                    ICollectionView view = CollectionViewSource.GetDefaultView(value);

                    patientListView.ItemsSource = view;
                }
            }
        }

        public QueryInfoListPad()
        {
            InitializeComponent();
        }

        private void patientListView_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            object item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));

            bool isItemSelected = (item != null);
            if (isItemSelected)
            {
                ContextMenu ctxMenu = new ContextMenu();
                MenuItem mItem = new MenuItem();
                mItem.Header = "添加";
                mItem.Click += this.AddPrint;
                mItem.Height = 25;
                mItem.Width = 120;
                ctxMenu.Items.Add(mItem);
                mItem = new MenuItem();
                mItem.Header = "取消";
                mItem.Click += this.RemovePrint;
                mItem.Height = 25;
                mItem.Width = 120;
                ctxMenu.Items.Add(mItem);

                this.patientListView.ContextMenu = ctxMenu;
  
            }
            else
            {
                this.patientListView.ContextMenu = null;
            }
        }

        /**
        * 功能：添加打印
        * 
        * */
        private void AddPrint(object sender, RoutedEventArgs e)
        {
            UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
            PatientInfo seletedItem = (PatientInfo)(item as ListView).SelectedItem;
            seletedItem.State = DataModel.RecordState.Printed;

            if (patientPrintList.IndexOf(seletedItem) < 0)
            {
                patientPrintList.Add(seletedItem);
            }
        }

        /**
       * 功能：取消打印
       * 
       * */
        private void RemovePrint(object sender, RoutedEventArgs e)
        {
            UIElement item = ContextMenuService.GetPlacementTarget(LogicalTreeHelper.GetParent(sender as MenuItem));
            PatientInfo seletedItem = (PatientInfo)(item as ListView).SelectedItem;
            seletedItem.State = DataModel.RecordState.Normal;

            if (patientPrintList.IndexOf(seletedItem) >= 0)
            {
                patientPrintList.Remove(seletedItem);
            }
        }

        private object GetElementFromPoint(ItemsControl itemsControl, Point point)
        {
            UIElement element = itemsControl.InputHitTest(point) as UIElement;
            while (element != null)
            {
                if (element == itemsControl)
                    return null;
                object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element);
                if (!item.Equals(DependencyProperty.UnsetValue))
                    return item;
                element = (UIElement)VisualTreeHelper.GetParent(element);
            }
            return null;
        }
    }
}
